package ru.spies.forms;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

import ru.spies.content.SystemContent;

public class SystemForm extends FormCreator {
	
	private static SystemForm statThis;
	
	private static IFormForWork formChooser;
			
	private SystemForm() {
		setCookieName(LastCookies.COOKIE_NAME_SYSTEM_FORM);
	}
	
	public static SystemForm getInstance(){
		if (statThis==null){
			statThis = new SystemForm();
		}
		return statThis;
	}

	@Override
	protected ICookie childCookieWork(Cookie cookie) {
		if (Integer.valueOf(cookie.getValue())==ESystemFunction.ADD_NEW_TABLE_OBJECT.ordinal()){
			return ESystemFunction.ADD_NEW_TABLE_OBJECT;
		}
		if (Integer.valueOf(cookie.getValue())==ESystemFunction.SHOW_OBJECTS.ordinal()){
			return ESystemFunction.SHOW_OBJECTS;
		}
		if (Integer.valueOf(cookie.getValue())==ESystemFunction.DB_FUNCTIONAL.ordinal()){
			return ESystemFunction.DB_FUNCTIONAL;
		}
		if (Integer.valueOf(cookie.getValue())==ESystemFunction.DELETE_OBJECT_FROM_TABLE.ordinal()){
			return ESystemFunction.DELETE_OBJECT_FROM_TABLE;
		}
		if (Integer.valueOf(cookie.getValue())==ESystemFunction.UPDATE_OBJECT.ordinal()){
			return ESystemFunction.UPDATE_OBJECT;
		}
		return null;
	}

	@Override
	protected String workWithCookieValue(ICookie cook) {
		ESystemFunction func = (ESystemFunction) cook;
		LastCookies.getInstance().setLastSystemCookie(func);
		switch (func) {
		case ADD_NEW_TABLE_OBJECT:
			formChooser = new IFormForWork() {
				
				@Override
				public String doMainFormWork(HttpServletRequest request) {
					return AddForm.getInstance().chooseFunctionForWork(request);
				}
				
			};
			return AddForm.getInstance().createThisForm();
		case DB_FUNCTIONAL:
			formChooser = new IFormForWork() {
				
				@Override
				public String doMainFormWork(HttpServletRequest request) {
					return DBFunctionalForm.getInstance().chooseFunctionForWork(request);
				}
				
			};
			return DBFunctionalForm.getInstance().createThisForm();
		case DELETE_OBJECT_FROM_TABLE:
			formChooser = new IFormForWork() {
				
				@Override
				public String doMainFormWork(HttpServletRequest request) {
					return DeleteForm.getInstance().chooseFunctionForWork(request);
				}
				
			};
			return DeleteForm.getInstance().createThisForm();
		case SHOW_OBJECTS:
			formChooser = new IFormForWork() {
				
				@Override
				public String doMainFormWork(HttpServletRequest request) {
					return ShowForm.getInstance().chooseFunctionForWork(request);
				}
				
			};
			return ShowForm.getInstance().createThisForm();
		case UPDATE_OBJECT:
			formChooser = new IFormForWork() {
				
				@Override
				public String doMainFormWork(HttpServletRequest request) {
					return UpdateForm.getInstance().chooseFunctionForWork(request);
				}
			};
			return UpdateForm.getInstance().createThisForm();
		default:
			
			return SOME_ERRORS;
		}
	}
	
	public IFormForWork getFormForWork(){
		return formChooser;
	}

	@Override
	public String createThisForm() {
		return createForm(SystemContent.getInstance().formSystemContent());
	}
	
	public String getErrorMessage(){
		return createForm(SOME_ERRORS);
	}
	
}
